'use strict';

/**
 * @ngdoc function
 * @name afasApp.controller:SupplierCtrl
 * @description
 * # SupplierCtrl
 * Controller of the afasApp
 */
angular.module('afasApp')
  .controller('SuppliersCtrl', ['$scope', '$state', 'Services', function ($scope, $state, Services) {

    $scope.suppliers = [];
    $scope.newItem = {};

    function applyRemoteData( newData ) {
      $scope.suppliers = newData;
    }

    function loadRemoteData() {
      Services.getItems('suppliers')
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.add = function () {
      // console.log($scope.newItem);
      Services.add('suppliers', $scope.newItem)
      .then(function(results){
        // console.log(results);
        $scope.newItem = {};
        $state.go( 'suppliers.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.delete = function (item) {
      Services.delete('suppliers', item);
    };
  }])
  .controller('SupplierCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.current = {};
    $scope.currentID = $stateParams.id;
    $scope.current = Services.getItem('suppliers', $stateParams.id);

    $scope.update = function () {
      Services.update('suppliers', $scope.current, $scope.currentID);
      $state.go( 'suppliers.show', {id: $scope.currentID} );
    };
  }]);
